package org.example.lottery_system.common.interceptor;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.example.lottery_system.common.utils.JWTUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

@Configuration
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userToken = request.getHeader("user_token");
        Claims claims = JWTUtil.parseJWT(userToken);
        if(claims == null){
            log.info("token解析错误");
            response.setStatus(401);
            return false;
        }
        log.info("token解析成功");
        Integer userId = JWTUtil.getUserIdFromToken(userToken);

        return true;
    }


}
